package com.lanou3g.zjl.chating.home.friends;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMGroup;
import com.easemob.chat.EMGroupInfo;
import com.easemob.chat.EMGroupManager;
import com.easemob.exceptions.EaseMobException;
import com.lanou3g.zjl.chating.R;
import com.lanou3g.zjl.chating.base.BaseActivity;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by dllo on 15/11/24.
 */
public class FindPublicGroupActivity extends BaseActivity {
    @Bind(R.id.groupId)
    EditText groupId;
    @Bind(R.id.findGroupName)
    TextView findGroupName;
    @Bind(R.id.findNow)
    TextView findNow;
    @Bind(R.id.theGroupFind)
    LinearLayout theGroupFind;
    private EMGroup group;

    @Override
    protected int getLayout() {

        return R.layout.find_public_group;
    }

    @Override
    protected void initData() {
        //隐藏显示群信息控件
        theGroupFind.setVisibility(View.INVISIBLE);
    }
    @OnClick(R.id.backToOpenGroup) void backToOpenGroup(){
        finish();
    }
    //点击查询按钮查找群
    @OnClick(R.id.findNow)
    void toFind() {
        if (!TextUtils.isEmpty(groupId.getText().toString().trim())) {
            new Thread(new Runnable() {

                @Override
                public void run() {

                    try {
                        group = EMGroupManager.getInstance().getGroupFromServer(groupId.getText().toString().trim());

                    } catch (EaseMobException e) {
                        e.printStackTrace();
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if(group == null){
                                Toast.makeText(getApplicationContext(), "该群不存在!!", Toast.LENGTH_SHORT).show();
                            }else {
                                if (!group.getOwner().equals(EMChatManager.getInstance().getCurrentUser())&& group.isPublic()) {
                                    Log.i("lanou","这个群是私有的还是公开的"+group.isPublic());
                                    theGroupFind.setVisibility(View.VISIBLE);
                                    findGroupName.setText(group.getGroupName());
                                } else {
                                    Toast.makeText(getApplicationContext(), "该群为私有群!!", Toast.LENGTH_SHORT).show();
                                }
                            }

                        }
                    });
                }
            }).start();
        } else {

            Toast.makeText(getApplicationContext(), "群号不能为空!!", Toast.LENGTH_SHORT).show();
        }

    }

    //点击查到的群跳转到添加公开群页面
    @OnClick(R.id.theGroupFind)
    void gotoAddTheGroup() {
        EMGroupInfo info = new EMGroupInfo(group.getGroupId(), group.getGroupName());
        Intent intent = new Intent(FindPublicGroupActivity.this, AddPublicGroupsActivity.class);
        intent.putExtra("groupInfo", info);
        startActivity(intent);
    }
}
